package sia.tpIntro;

import java.lang.reflect.Field;

public class SimulationParams {
	
	public int precision = 62;
	public int initialPop = 500;
	public int generationCount = 10;
	public int selectionCount = 10;
	public double mutationP = 0.03;
	
	SimulationParams configWith(String args[]) throws NumberFormatException, IllegalArgumentException, IllegalAccessException{

		String split[] = null;
		for (String arg : args) {
			split = arg.split("=");
			if(split.length >= 2) {
				try {
					Field aField = SimulationParams.class.getField(split[0]);
					if(aField.getType() == double.class) {
						aField.setDouble(this, new Double(split[1]));
					} else {
						aField.setInt(this, new Integer(split[1]));
					}
				} catch (NoSuchFieldException e) {
					System.out.println("No such argument : " + split[0] + ", Using default");
				}
			}
		}
		return this;
	}
	
	@Override
	public String toString() {
		return String.format("precision = %d; initialPop = %d; generationCount = %d; selectionCount = %d; mutationP = %g",
				precision, initialPop, generationCount, selectionCount, mutationP);
	}
}
